
package com.csc440.team2.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

import com.csc440.team2.SmilConstants;
import com.csc440.team2.database.entities.FolderType;
import com.csc440.team2.database.DBAdapter;
import com.csc440.team2.database.entities.Message;
import com.csc440.team2.smil.SMILMessage;
import com.csc440.team2.smil.SMILReadException;
import com.csc440.team2.smil.SMILReader;


public class InboxTab extends ListActivity
{
	private SMILMessage message;
	protected ListView lv;
	protected static final int CONTEXTMENU_DELETEITEM = Menu.FIRST;
	protected static final int CONTEXTMENU_DELETEALL = (Menu.FIRST+1);
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<SMILMessage>(this, R.layout.inbox_menu, getSMILMessages()));

        lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                // When clicked, show a toast with the TextView text
                message = (SMILMessage) getSMILMessages().get(position);
                Intent myIntent = new Intent(view.getContext(), SMILPlayer.class);
                myIntent.putExtra(SmilConstants.MESSAGE, getSMILMessages().get(position));
                startActivity(myIntent);
            }
        });
        lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { 

            public void onCreateContextMenu(ContextMenu conMenu, View arg1, ContextMenu.ContextMenuInfo menuInfo) { 
                 conMenu.setHeaderTitle("Menu"); 
                 conMenu.add(Menu.NONE, CONTEXTMENU_DELETEITEM, 1, "Delete");
                 conMenu.add(Menu.NONE, CONTEXTMENU_DELETEALL, 2, "Delete All");
                 /* Add as many context-menu-options as you want to. */ 
            } 
       });
        
    }
    //not working
    private void deleteMessage()
    {
        try
        {
            Message dbMessage;

            dbMessage = new Message(message.getXml(), FolderType.INBOX, true, "");

            if (dbMessage.delete(this))
                Log.i("Delete", "Message Deleted");
            else
                Log.i("Delete", "Failed to delete message");

        }
        catch (SMILReadException e)
        {
            Log.e("Delete", "Could not delete message", e);
        }
    }
    public boolean onContextItemSelected(MenuItem aItem) { 
    	AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) aItem.getMenuInfo();

        /* Switch on the ID of the item, to get what the user selected. */ 
        switch (aItem.getItemId()) { 
             case CONTEXTMENU_DELETEITEM:
            	 //not working
            	 deleteMessage();	
            	 
            	 return true; /* true means: "we handled the event". */
            	 
             case CONTEXTMENU_DELETEALL:
            	 return true; /* true means: "we handled the event". */
                  
                   
        } 
        return false; 
   } 
    
    
    private List<SMILMessage> getSMILMessages()
    {
        List<SMILMessage> messages = new ArrayList<SMILMessage>();
        try
        {
            List<Message> messageList = Message.getAll(this);

            int counter = 1;
            for (Message currentMessage : messageList)
            {
                if (currentMessage.getFolder() == FolderType.INBOX)
                {
                    SMILMessage smilMessage = SMILReader.parseMessage(currentMessage.getMessage());
                    smilMessage.setSubject("Message " + counter);
                    messages.add(smilMessage);
                }

                ++counter;
            }
        }
        catch (SMILReadException e)
        {
            Log.e("Get Messages", "Could not retrieve SMIL messages", e);
        }
        return messages;
    }

}
